import {
  Body,
  Controller,
  Get,
  Param,
  ParseFilePipeBuilder,
  ParseIntPipe,
  Post,
  Query,
  UploadedFile,
  UseInterceptors,
} from '@nestjs/common';
import { BookService } from '@/modules/book/book.service';
import { wrapperCountResponse, wrapperResponse } from '@/utils';
import { FileInterceptor } from '@nestjs/platform-express';
import fs from 'fs';
import path from 'path';

@Controller('book')
export class BookController {
  constructor(private readonly bookService: BookService) {}

  @Get()
  getBookList(@Query() query) {
    return wrapperCountResponse(
      this.bookService.getBookList(query),
      this.bookService.countBookList(query),
      '查询电子书列表成功',
    );
  }

  @Get('/:id')
  getBook(@Param('id', ParseIntPipe) id: number) {
    return wrapperResponse(
      this.bookService.getBookById(id),
      '获取电子书详情成功',
    );
  }

  // 提交电子书
  @Post()
  insertBook(@Body() body) {
    return wrapperResponse(this.bookService.addBook(body), '新增电子书成功');
  }

  // 上传文件
  @Post('upload')
  @UseInterceptors(FileInterceptor('file'))
  uploadBook(
    @UploadedFile()
    // new ParseFilePipeBuilder()
    //   .addFileTypeValidator({
    //     fileType: 'jpeg',
    //   })
    //   .build(),
    file,
  ) {
    return wrapperResponse(this.bookService.uploadBook(file), '上传文件成功');
  }
}
